//
//  LookRoomViewController.m
//  HouseShow
//
//  Created by gongjie on 11-12-26.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "LookRoomViewController.h"
#import "ASIFormDataRequest.h"
@implementation LookRoomViewController
@synthesize name;
@synthesize coll;
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{

    [self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f]];
    
    UIView* zhongjian = [[UIView alloc] initWithFrame:CGRectMake(240, 0, 540, 1024)];
    [zhongjian setBackgroundColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0f]];
    UIView* hend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 44)];
    [hend setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"hand.png"]]];
    
    UIButton* Lbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    Lbutton.frame = CGRectMake(10, 4, 60, 34);
    [Lbutton setTitle:@"返回" forState:UIControlStateNormal];
    [Lbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[Lbutton addTarget:self action:@selector(changerightBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    [hend addSubview:Lbutton];
	
	
    UIButton* ibutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ibutton.frame = CGRectMake(470, 4, 60, 34);
    [ibutton setTitle:@"提交" forState:UIControlStateNormal];
    [ibutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [ibutton addTarget:self action:@selector(changeleftBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    [hend addSubview:ibutton];
    [zhongjian addSubview:hend];
    [hend release];
   
    content = [[UITableView alloc] initWithFrame:CGRectMake(15, 80, 500, 150) style:UITableViewStyleGrouped];
    [content setDelegate:self];
    [content setDataSource:self];
    
    [zhongjian addSubview:content];
    
    
    tip = [[UILabel alloc] initWithFrame:CGRectMake(30, 70, 250, 22)];
    [tip setBackgroundColor:[UIColor clearColor]];
    tip.font = [UIFont systemFontOfSize:20.0f];
    tip.text = @"请输入您的姓名和手机号";
    tip.textColor = [UIColor blackColor];
    [zhongjian addSubview:tip];
    description = [[UILabel alloc] initWithFrame:CGRectMake(90, 245, 350, 17)];
    [description setBackgroundColor:[UIColor clearColor]];
    description.font = [UIFont systemFontOfSize:15.0];
    description.text = @"收到您的信息，我们会尽快联系您，感谢你的支持。";
    description.textColor = [UIColor colorWithRed:93/255 green:102/255 blue:121/255 alpha:1.0f];
    [zhongjian addSubview:description];
    
    [self.view addSubview:zhongjian];
    name = [[UITextField alloc] initWithFrame:CGRectMake(70, 18, 300, 60)];
    [name becomeFirstResponder];
    coll = [[UITextField alloc] initWithFrame:CGRectMake(70, 18, 300, 60)];

}
-(void) changerightBarButtonItem
{
    [coll resignFirstResponder];
    [name resignFirstResponder];
    coll.text =@"";
    name.text = @"";
    self.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.2];
    self.view.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];
}
-(void) changeleftBarButtonItem
{
    if (name.text!=nil&&name.text!=@""&&coll.text!=nil&&coll.text!=@"") {
        NSString* date = [[NSString alloc] initWithFormat:@"mobile=%@&name=%@",coll.text,name.text];
        [self requestsubmit:date];
    }else{
        UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入正确的文字" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        tAlertView.tag =1004;
        [tAlertView show];
        [tAlertView release];
    }
}
-(void)requestsubmit:(NSString*)data 
{
    NSLog(@"dasdfas=%@",data);

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.unitrue.com.cn:8088/ClientWebPage/CBespoke.aspx?"]];
    [request setDelegate:self];
    [request setPostValue:coll.text forKey:@"mobile"];
    [request setPostValue:name.text forKey:@"name"];
    [request setDidFailSelector:@selector(requestsubmitFailure:)];
    [request setDidFinishSelector:@selector(requestsubmitSuccess:)];
    [request startAsynchronous];}
-(void)requestsubmitSuccess:(ASIHTTPRequest*)request
{
    NSArray* tCookieArr = [request responseCookies];
    NSString* tFailStr = nil;
    NSString* IsFail = nil;
    for (NSHTTPCookie* tOneCookie in tCookieArr) {
        if ([tOneCookie.name isEqualToString:@"opresult"]) {
            IsFail = tOneCookie.value;
        }else if([tOneCookie.name isEqualToString:@"failreason"]){
            tFailStr = tOneCookie.value;
        }
    }
    if ([IsFail boolValue]) {
        UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交成功" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        tAlertView.tag =1000;
        [tAlertView show];
        [tAlertView release];

    }else{
        UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:tFailStr delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        tAlertView.tag =1001;
        [tAlertView show];
        [tAlertView release];
    }
    
}
-(void)requestsubmitFailure:(ASIHTTPRequest*)request
{
    NSLog(@"dasdaafas");
    name.text =@"";
    coll.text =@"";
    UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败，请检查网络后重新运行！" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
    tAlertView.tag =1002;
    [tAlertView show];
    [tAlertView release];
}
#pragma UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView* tview = [[[UIView alloc] initWithFrame:tableView.frame] autorelease];
	[tview setBackgroundColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0]];
	[tableView setBackgroundView:tview];
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
        UILabel* tip1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 18, 50, 22)];
        tip1.tag = 1;
        [tip1 setBackgroundColor:[UIColor clearColor]];
        tip1.font = [UIFont systemFontOfSize:20.0f];
        tip1.textColor = [UIColor blackColor];
        [cell.contentView addSubview:tip1];
        
        if ([indexPath row] == 0) {
            
            [name setBorderStyle:UITextBorderStyleNone];
            [name setPlaceholder:@""];
            [name setReturnKeyType:UIReturnKeyDone];
            [name setFont:[UIFont systemFontOfSize:20]];
            name.keyboardType = UIKeyboardTypeNamePhonePad;
            [cell.contentView addSubview:name];
        }
        else
        {
        
            [coll setBorderStyle:UITextBorderStyleNone];
            [coll setPlaceholder:@""];
            [coll setReturnKeyType:UIReturnKeyDone];
            [coll setFont:[UIFont systemFontOfSize:20]];
            coll.keyboardType = UIKeyboardTypeNumberPad;
            [cell.contentView addSubview:coll];
        }
       
        
    }
      UILabel* ilabel = (UILabel*)[cell.contentView viewWithTag:1];
    if ([indexPath row] == 0) {
        [ilabel setText:@"姓名:"];
        
    }
    else
    {
        [ilabel setText:@"电话:"];
    }
    // Configure the cell...
    
    return cell;
}

#pragma UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(void)dealloc{
    [tip release];
    [description release];
    [content release];
    [name release];
    [coll release];
    [super dealloc];
}
@end
